package com.ruoyi.web.controller.set;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SetConstmerPartRelation;
import com.ruoyi.system.service.ISetConstmerPartRelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户产品关联Controller
 *
 * @author ruoyi
 * @date 2023-02-23
 */
@Api(tags = "客户产品关联api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/set/relation" )
public class SetConstmerPartRelationController extends BaseController {

    private final ISetConstmerPartRelationService iSetConstmerPartRelationService;

    /**
     * 查询客户产品关联列表
     */
    @ApiOperation(value = "查询客户产品关联列表")
    //@PreAuthorize("@ss.hasPermi('set:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(SetConstmerPartRelation setConstmerPartRelation)
    {
        startPage();
        LambdaQueryWrapper<SetConstmerPartRelation> lqw = new LambdaQueryWrapper<SetConstmerPartRelation>();
        if (StringUtils.isNotBlank(setConstmerPartRelation.getCustomerCode())){
            lqw.eq(SetConstmerPartRelation::getCustomerCode ,setConstmerPartRelation.getCustomerCode());
        }
        if (StringUtils.isNotBlank(setConstmerPartRelation.getCustomerName())){
            lqw.like(SetConstmerPartRelation::getCustomerName ,setConstmerPartRelation.getCustomerName());
        }
        if (null!=setConstmerPartRelation.getModelId()){
            lqw.eq(SetConstmerPartRelation::getModelId ,setConstmerPartRelation.getModelId());
        }
        List<SetConstmerPartRelation> list = iSetConstmerPartRelationService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出客户产品关联列表
     */
    //@PreAuthorize("@ss.hasPermi('set:relation:export')" )
    @Log(title = "客户产品关联" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(SetConstmerPartRelation setConstmerPartRelation) {
        LambdaQueryWrapper<SetConstmerPartRelation> lqw = new LambdaQueryWrapper<SetConstmerPartRelation>(setConstmerPartRelation);
        List<SetConstmerPartRelation> list = iSetConstmerPartRelationService.list(lqw);
        ExcelUtil<SetConstmerPartRelation> util = new ExcelUtil<SetConstmerPartRelation>(SetConstmerPartRelation. class);
        return util.exportExcel(list, "relation" );
    }

    /**
     * 获取客户产品关联详细信息
     */
    @ApiOperation(value = "获取客户产品关联详细信息")
    //@PreAuthorize("@ss.hasPermi('set:relation:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iSetConstmerPartRelationService.getById(id));
    }

    /**
     * 新增客户产品关联
     */
    @ApiOperation(value = "新增客户产品关联")
    //@PreAuthorize("@ss.hasPermi('set:relation:add')" )
    @Log(title = "客户产品关联" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SetConstmerPartRelation setConstmerPartRelation) {
        setConstmerPartRelation.setCreateBy(SecurityUtils.getUsername());
        setConstmerPartRelation.setCreateTime(new Date());
        return toAjax(iSetConstmerPartRelationService.save(setConstmerPartRelation) ? 1 : 0);
    }

    /**
     * 修改客户产品关联
     */
    @ApiOperation(value = "修改客户产品关联")
    //@PreAuthorize("@ss.hasPermi('set:relation:edit')" )
    @Log(title = "客户产品关联" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SetConstmerPartRelation setConstmerPartRelation) {
        setConstmerPartRelation.setUpdateBy(SecurityUtils.getUsername());
        setConstmerPartRelation.setUpdateTime(new Date());
        return toAjax(iSetConstmerPartRelationService.updateById(setConstmerPartRelation) ? 1 : 0);
    }

    /**
     * 删除客户产品关联
     */
    @ApiOperation(value = "修改客户产品关联")
    //@PreAuthorize("@ss.hasPermi('set:relation:remove')" )
    @Log(title = "客户产品关联" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSetConstmerPartRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
